﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace WorkDAL
{
    public class WorksDataAccess
    {
        WorkerDataContext workContext = new WorkerDataContext();

        public static WorksDataAccess Instance()
        {
            return new WorksDataAccess();
        }

        public List<Work> GetWorkList(DateTime? openDate, DateTime? endDate, DateTime? expectedDate, string status, string user)
        {
            string[] statuslist = status.Split(',');
            var worklist = from c in this.workContext.Works
                           where (openDate == null || c.OpenDate == openDate)
                           && (endDate == null || c.EndDate == endDate)
                           && (expectedDate == null || c.ExpectedDate == expectedDate)
                           && (c.User == user)
                           && (status == "ALL" || statuslist.Contains(c.Status))
                           orderby c.OpenDate
                           select c;

            return worklist.ToList();
        }

        public Work GetWork(Guid Id)
        {
            var work = from c in workContext.Works
                       where c.ID == Id
                       select c;

            return (Work)work;
        }

        public void DeleteWork(Guid Id)
        {
            Work work = workContext.Works.Single(p => p.ID == Id);
            work.Status = "X";
            workContext.SubmitChanges();
        }

        public void CreateWork(Work item)
        {
            workContext.Works.InsertOnSubmit(item);
            workContext.SubmitChanges();
        }

        public void UpdateWork(Work item)
        {
            Work work = workContext.Works.Single(p => p.ID == item.ID);
            work.Descriptions = item.Descriptions;
            work.EndDate = item.EndDate;
            work.ExpectedDate = item.ExpectedDate;
            work.OpenDate = item.OpenDate;
            work.Status = item.Status;
            work.Title = item.Title;
            work.User = item.User;

            workContext.SubmitChanges();
        }

        public void UpdateStatus(Work item, string status)
        {
            if (status == string.Empty)
                return;

            Work work = workContext.Works.Single(p => p.ID == item.ID);
            work.Status = status;

            //if (status == "E" && item.EndDate != null)
            //    if (DisplayMessage.ShowConfirm(string.Format("Bạn có muốn cập nhật ngày hoàn thành là {0}", DateTime.Today.ToString("dd/MM/yyyy"))))
            //        work.EndDate = DateTime.Today;
            
            if(work.EndDate == null && status == "E")
                work.EndDate = DateTime.Today;

            workContext.SubmitChanges();
        }

        public List<TypeStatus> GetTypes(string type)
        {
            var typelist = from c in workContext.TypeStatus
                           where (type == string.Empty || c.Types == type)
                           select c;

            return typelist.ToList();
        }

    }
}
